package com.itlong.cloud.controller.app;

import com.itlong.cloud.POJO.DTO.operate.OperatePropertyPackageDTO;
import com.itlong.cloud.POJO.PO.PropertyPackagePO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyFunctionVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.noncore.operate.IOperatePropertyPackageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.operate.IOperatePropertyAccountServiceClient;
import com.itlong.cloud.user.property.IPropertyFunctionServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营-套餐管理-物业套餐管理控制器
 * </desc>
 *
 * @createDate2020/3/20
 **/
@RestController
@RequestMapping("/packageManage/propertyPackage")
public class OperatePropertyPackageController extends BaseController {

    @Autowired
    private IOperatePropertyPackageServiceClient iOperatePropertyPackageServiceClient;

    @Autowired
    private IOperatePropertyAccountServiceClient iOperatePropertyAccountServiceClient;

    @Autowired
    private IPropertyFunctionServiceClient iPropertyFunctionServiceClient;

    /**
     * <desc>
     *      分页获取物业套餐
     * </desc>
     *
     * @param operatePropertyPackageDTO 参数实体
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(value = "/getPackagePage",method = RequestMethod.POST)
    public Object getPackagePage(OperatePropertyPackageDTO operatePropertyPackageDTO, HttpServletRequest request){
        try{
            operatePropertyPackageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperatePropertyPackageDTO.class,request);
            Page<PropertyPackagePO> page = iOperatePropertyPackageServiceClient.getPage(operatePropertyPackageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-分页获取物业套餐getPackagePage】分页获取物业套餐失败",e);
        }
    }

    /**
     * <desc>
     *      获取单个物业套餐信息
     * </desc>
     *
     * @param propertyPackageId 物业套餐id
     * @param request
     * @return 单个物业套餐详细信息
     * @Author Zhu.yj
     * @createDate  2020/4/1
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(value = "/getOne",method = RequestMethod.POST)
    public Object getOne(String propertyPackageId,HttpServletRequest request){
        try{
            propertyPackageId = request.getAttribute("propertyPackageId") == null ? null : request.getAttribute("propertyPackageId").toString();
            if (StringUtils.isBlank(propertyPackageId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            PropertyPackagePO propertyPackagePO = iOperatePropertyPackageServiceClient.getOne(propertyPackageId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),propertyPackagePO);
        }catch (Exception e){
            throw new DataAccessException("【运营_套餐管理_获取单个物业套餐信息getOne】获取单个物业套餐失败",e);
        }
    }

    /**
     * <desc>
     *      更新物业套餐信息
     * </desc>
     *
     * @param propertyPackagePO 参数实体
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_UPDATE_PROPERTY_PACKAGE_LOG_DESC)
    @RequestMapping(value = "/updatePackage",method = RequestMethod.POST)
    public Object updatePackage(PropertyPackagePO propertyPackagePO,HttpServletRequest request){
        try{
            propertyPackagePO = HttpProtocolUtil.parseRequestParamToDTO(PropertyPackagePO.class,request);
            if (StringUtils.isBlank(propertyPackagePO.getPropertyPackageId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iOperatePropertyPackageServiceClient.update(propertyPackagePO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-更新物业套餐信息updatePackage】更新物业套餐信息失败",e);
        }
    }

    /**
     * <desc>
     *      添加物业套餐信息
     * </desc>
     *
     * @param propertyPackagePO 物业套餐实体
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_SAVE_PROPERTY_PACKAGE_LOG_DESC)
    @RequestMapping(value = "/savePackage",method = RequestMethod.POST)
    public Object savePackage(PropertyPackagePO propertyPackagePO,HttpServletRequest request){
        try {
            propertyPackagePO = HttpProtocolUtil.parseRequestParamToDTO(PropertyPackagePO.class,request);
            if (StringUtils.isBlank(propertyPackagePO.getPropertyPackageName()) || propertyPackagePO.getPackageType() == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(propertyPackagePO.getPackageType(),1,2)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iOperatePropertyPackageServiceClient.save(propertyPackagePO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-添加物业套餐信息savePackage】添加物业套餐信息失败",e);
        }
    }

    /**
     * <desc>
     *      删除物业套餐
     * </desc>
     *
     * @param propertyPackageIds 物业套餐id，多个逗号隔开
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DELETE_PROPERTY_PACKAGE_LOG_DESC)
    @RequestMapping(value = "/deletePackage",method = RequestMethod.POST)
    public Object deletePackage(String propertyPackageIds,HttpServletRequest request){
        try {
            propertyPackageIds = request.getAttribute("propertyPackageIds") == null ? null : request.getAttribute("propertyPackageIds").toString();
            if (StringUtils.isBlank(propertyPackageIds)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iOperatePropertyPackageServiceClient.delete(propertyPackageIds);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-删除物业套餐deletePackage】删除物业套餐失败",e);
        }
    }

//    /**
//     * <desc>
//     *      获取物业账号列表
//     * </desc>
//     *
//     * @param propertyPackageId 物业套餐id
//     * @param request
//     * @return
//     * @Author Zhu.yj
//     * @createDate  2020/3/25
//     */
//    @ResponseBody
//    @RequestMapping(value = "/getAccountPage",method = RequestMethod.POST)
//    public Object getAccountPage(OperatePropertyAccountGetPageDTO operatePropertyAccountGetPageDTO, String propertyPackageId, HttpServletRequest request){
//        try{
//            propertyPackageId = request.getAttribute("propertyPackageId") == null ? null : request.getAttribute("propertyPackageId").toString();
//            operatePropertyAccountGetPageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperatePropertyAccountGetPageDTO.class,request);
//            if (StringUtils.isBlank(propertyPackageId)){
//                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
//            }
//            Page<OperatePropertyAccountVO> page = iOperatePropertyAccountServiceClient.getAdminPage(operatePropertyAccountGetPageDTO);
//            List<String> accountList = iOperatePropertyPackageServiceClient.getPropertyPackageAccount(propertyPackageId);
////            List<String> otherList = iOperatePropertyPackageServiceClient.getOther(propertyPackageId);
//            page.getRows().forEach(row->{
//                row.setHaveAuth(0);
//                if(accountList.stream().filter(
//                        account->account.equals(row.getUserId())
//                ).findFirst().isPresent()){
//                    row.setHaveAuth(1);
//                }
//            });
//            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
//
//        }catch (Exception e){
//            throw new DataAccessException("【运营-套餐管理-获取物业账号列表getAccountPage】获取物业账号列表失败",e);
//        }
//    }

//    /**
//     * <desc>
//     *      获取套餐列表
//     * </desc>
//     *
//     * @param projectId 项目id
//     * @param request
//     * @return
//     * @Author Zhu.yj
//     * @createDate  2020/3/25
//     */
//    @ResponseBody
//    @RequestMapping(value = "/getPackageList",method = RequestMethod.POST)
//    public Object getPackageList(String projectId,HttpServletRequest request){
//        try {
//            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
//            if (StringUtils.isBlank(projectId)){
//                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
//            }
//            List<OperatePropertyPackageVO> operatePropertyPackageVOS = iOperatePropertyPackageServiceClient.getPackage(projectId);
//            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operatePropertyPackageVOS);
//        }catch (Exception e){
//            throw new DataAccessException("【运营-套餐管理-获取账号下套餐列表getPackage】获取套餐列表失败",e);
//        }
//    }

    /**
     * <desc>
     *      获取物业菜单
     * </desc>
     *
     * @param propertyPackageIds 物业套餐id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @ResponseBody
    @RequestMapping(value = "/getAllotFunction",method = RequestMethod.POST)
    public Object getAllotFunction(String propertyPackageIds,HttpServletRequest request){
        try{
            propertyPackageIds = request.getAttribute("propertyPackageIds") == null ? null : request.getAttribute("propertyPackageIds").toString();
            if (StringUtils.isBlank(propertyPackageIds)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<PropertyFunctionVO> propertyFunctionVOS = iPropertyFunctionServiceClient.getPackageFunction(propertyPackageIds);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),propertyFunctionVOS);
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-获取物业套餐getAllotFunction】获取物业套餐失败",e);
        }
    }

    /**
     * <desc>
     *      物业套餐分配功能
     * </desc>
     *
     * @param propertyFunctionIds 物业功能id，多个逗号隔开
     * @param propertyPackageId
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_PROPERTY_PACKAGE_ALLOT_FUNCTION_LOG_DESC)
    @RequestMapping(value = "/allotFunction",method = RequestMethod.POST)
    public Object allotPropertyFunction(String propertyFunctionIds,String propertyPackageId,HttpServletRequest request){
        try{
            propertyFunctionIds = request.getAttribute("propertyFunctionIds") == null ? null : request.getAttribute("propertyFunctionIds").toString();
            propertyPackageId = request.getAttribute("propertyPackageId") == null ? null : request.getAttribute("propertyPackageId").toString();
            if (StringUtils.isBlank(propertyFunctionIds) || StringUtils.isBlank(propertyPackageId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iOperatePropertyPackageServiceClient.allotPropertyFunction(propertyFunctionIds,propertyPackageId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());

        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-物业套餐分配功能allotPropertyFunction】物业套餐分配功能失败",e);
        }
    }

//    /**
//     * <desc>
//     *      物业套餐分配用户
//     * </desc>
//     *
//     * @param propertyPackageId 物业套餐id
//     * @param allotUserIds 要分配的userId
//     * @param userIds 当前页所有userId
//     * @return
//     * @Author Zhu.yj
//     * @createDate  2020/3/26
//     */
//    @ResponseBody
//    @AuthAccessRequired
//    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_PROPERTY_PACKAGE_ALLOT_ACCOUNT_LOG_DESC)
//    @RequestMapping(value = "/allotAccounts",method = RequestMethod.POST)
//    public Object allotAccounts(String propertyPackageId, String allotUserIds,String userIds,HttpServletRequest request){
//        try {
//            propertyPackageId = request.getAttribute("propertyPackageId") == null ? null : request.getAttribute("propertyPackageId").toString();
//            allotUserIds = request.getAttribute("allotUserIds") == null ? null : request.getAttribute("allotUserIds").toString();
//            userIds = request.getAttribute("userIds") == null ? null : request.getAttribute("userIds").toString();
//            if (StringUtils.isBlank(propertyPackageId)|| StringUtils.isBlank(userIds)){
//                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
//            }
//            iOperatePropertyPackageServiceClient.allotPackageUser(propertyPackageId,allotUserIds,userIds);
//            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
//        }catch (Exception e){
//            throw new DataAccessException("【运营_套餐管理_物业套餐分配用户allotPropertyUser】物业套餐分配用户失败",e);
//        }
//    }
}
